package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.order.IOrderPayInfoService;
import com.mdd.admin.validate.order.OrderPayInfoCreateValidate;
import com.mdd.admin.validate.order.PayInfoSearchValidate;
import com.mdd.admin.validate.order.PayInfoUpdateValidate;
import com.mdd.admin.vo.order.OrderPayInfoDetailVo;
import com.mdd.admin.vo.order.OrderPayInfoVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.OrderPayInfo;
import com.mdd.common.mapper.order.OrderPayInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 订单支付信息实现类
 * @author LikeAdmin
 */
@Service
public class OrderPayInfoServiceImpl implements IOrderPayInfoService {

    @Resource
    OrderPayInfoMapper orderPayInfoMapper;

    /**
     * 订单支付信息列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<OrderPayInfoListedVo>
     */
    @Override
    public PageResult<OrderPayInfoVo> list(PageValidate pageValidate, PayInfoSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<OrderPayInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        orderPayInfoMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:orderNo@order_no:str",
            "=:payType@pay_type:long",
            "=:payMony@pay_mony:str",
            "=:payTime@pay_time:str",
            "=:discountNo@discount_no:str",
            "=:discountType@discount_type:int",
            "=:payStatus@pay_status:long",
            "=:payInfoNo@pay_info_no:str",
        });

        IPage<OrderPayInfo> iPage = orderPayInfoMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<OrderPayInfoVo> list = new LinkedList<>();
        for(OrderPayInfo item : iPage.getRecords()) {
            OrderPayInfoVo vo = new OrderPayInfoVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 订单支付信息详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OrderPayInfo
     */
    @Override
    public OrderPayInfoDetailVo detail(Long id) {
        OrderPayInfo model = orderPayInfoMapper.selectOne(
                new QueryWrapper<OrderPayInfo>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "订单支付信息数据不存在");

        OrderPayInfoDetailVo vo = new OrderPayInfoDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 订单支付信息新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(OrderPayInfoCreateValidate createValidate) {
        OrderPayInfo model = new OrderPayInfo();
        model.setOrderNo(createValidate.getOrderNo());
        model.setPayType(createValidate.getPayType());
        model.setPayMony(createValidate.getPayMony());
        model.setPayTime(createValidate.getPayTime());
        model.setDiscountNo(createValidate.getDiscountNo());
        model.setDiscountType(createValidate.getDiscountType());
        model.setPayStatus(createValidate.getPayStatus());
        model.setPayInfoNo(createValidate.getPayInfoNo());
        orderPayInfoMapper.insert(model);
    }

    /**
     * 订单支付信息编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(PayInfoUpdateValidate updateValidate) {
        OrderPayInfo model = orderPayInfoMapper.selectOne(
                new QueryWrapper<OrderPayInfo>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "订单支付信息数据不存在!");

        model.setId(updateValidate.getId());
        model.setOrderNo(updateValidate.getOrderNo());
        model.setPayType(updateValidate.getPayType());
        model.setPayMony(updateValidate.getPayMony());
        model.setPayTime(updateValidate.getPayTime());
        model.setDiscountNo(updateValidate.getDiscountNo());
        model.setDiscountType(updateValidate.getDiscountType());
        model.setPayStatus(updateValidate.getPayStatus());
        model.setPayInfoNo(updateValidate.getPayInfoNo());
        orderPayInfoMapper.updateById(model);
    }

    /**
     * 订单支付信息删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        OrderPayInfo model = orderPayInfoMapper.selectOne(
                new QueryWrapper<OrderPayInfo>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "订单支付信息数据不存在!");

        orderPayInfoMapper.delete(new QueryWrapper<OrderPayInfo>().eq("id", id));
    }



}
